package edu.calpoly.csc.coursebook.web;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import edu.calpoly.csc.coursebook.business.model.Course;
import edu.calpoly.csc.coursebook.business.model.CourseManager;
import edu.calpoly.csc.coursebook.util.impl.CategoryTablePopulator;

/****
 * an internal class that uses the spring framework to connect to the db and 
 * access all the courses.
 * 
 * @author bestrada
 */
public class CourseRetrieverController implements Controller
{
   /** Logger for this class and subclasses */
   protected final Log log = LogFactory.getLog(this.getClass());
   
   private CourseManager courseMan;
   
   public void setCourseManager(CourseManager cm)
   {
      this.courseMan = cm;
   }
   
   public CourseManager getCourseManager()
   {
      return this.courseMan;
   }

   public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
   {
      log.info("using the course retriever");
      
      Map<String, Object> myModel = new HashMap<String, Object>();
      Collection<Course> courses = this.getCourseManager().getCourses();
      
      /* call ryan's method here.... */
      CategoryTablePopulator ctp = new CategoryTablePopulator();
      ctp.populateCategoryTable(courses);
      
      return new ModelAndView("courses", "model", myModel);
   }
   
   
}
